package com.tongzhou.client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tongzhou.util.FileUtil;

import net.sf.json.JSONObject;

public class TongZhouServlet extends HttpServlet{
	private String json;
	private String method;
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        method=(String)request.getParameter("method");
        json=(String)request.getParameter("json");
        if(method!=null||json!=null){
        	try {
    			JSONObject jsonObject = JSONObject.fromObject( json );
//    			String method=(String) jsonObject.get( "method");
//    			String method=(String) jsonObject.get( "method");
    			String returnStr="";
    			if(method!=null){
    				if(method.equals("1")){
    					//登陆接口
    					ClientLogin login=new ClientLogin();
    					returnStr=login.getInfo(json,response);
    				}else if(method.equals("2")){
    					//注册接口
    					ClientRegist clientRegist=new ClientRegist();
    					returnStr=clientRegist.getInfo(json);
    				}else if(method.equals("3")){
    					//应用初始化接口
    					ClientInit clientInit=new ClientInit();
    					returnStr=clientInit.getInfo(json,response);
    				}else if(method.equals("4")){
    					Integer type=(Integer) jsonObject.get("type");
    					if(type!=null){
    						if(type==1){
    							//报价接口
    							ClientQuot clientQuot=new ClientQuot();
    							returnStr=clientQuot.getInfo(json);
    						}else if(type==2){
    							//报价接口html
    							ClientQuot2 clientQuot2=new ClientQuot2();
    							returnStr=clientQuot2.getInfo(json,request);
    						}else if(type==3){
    							//报价接口
    							ClientQuot3 clientQuot3=new ClientQuot3();
    							returnStr=clientQuot3.getInfo(json);
    						}
    					}
    				}else if(method.equals("5")){
    					//生成排期接口
    					ClientProduct clientProduct=new ClientProduct();
    					returnStr=clientProduct.getInfo(json);
    				}else if(method.equals("6")){
    					//物流查询接口
    					ClientQuery clientQuery=new ClientQuery();
    					returnStr=clientQuery.getInfo(json);
    				}else if(method.equals("7")){
    					//物流查询详细接口
    					ClientDetail clientDetail=new ClientDetail();
    					returnStr=clientDetail.getInfo(json);
    				}else if(method.equals("8")){
    					//同舟快讯接口
    					ClientNews clientNews=new ClientNews();
    					returnStr=clientNews.getInfo(json);
    				}else if(method.equals("9")){
    					//促销接口
    					ClientPromotion clientPromotion=new ClientPromotion();
    					returnStr=clientPromotion.getInfo(json);
    				}else if(method.equals("10")){
    					//升级、更新接口
    					ClientUpdate clientUpdate=new ClientUpdate();
    					returnStr=clientUpdate.getInfo(json,request);
    				}else if(method.equals("11")){
    					//留言接口
    					ClientMsg clientMsg=new ClientMsg();
    					returnStr=clientMsg.getInfo(json);
    				}
    			}else{
    				returnStr="{'success':0,'msg':'输入参数有误!'}";
    			}
    			out = response.getWriter();
    	        out.print(returnStr);
    	        out.flush();
    	        out.close();
    	        return ;
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
        }
		try {
			PrintWriter out2 = response.getWriter();
			out2.print("{'success':'0','msg':'输入参数有误！'}");
	        out2.flush();
	        out2.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
        return ; 
	}

	/**
	 * @return the json
	 */
	public String getJson() {
		return json;
	}

	/**
	 * @param json the json to set
	 */
	public void setJson(String json) {
		this.json = json;
	}

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method the method to set
	 */
	public void setMethod(String method) {
		this.method = method;
	}
}
